package com.example.idea.customer;

import com.example.idea.exception.ApiRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

@RequestMapping(path = "api/v2/customers")
@RestController
public class CustomerControllerV2 {
    private final CustomerService customerService;
@Autowired
    public CustomerControllerV2(CustomerService customerService) {
       this.customerService=customerService;
    }

    @GetMapping
    List<Customer> getCustomers() {
        return  customerService.getCustomers();
    }
    @GetMapping(path = "{customerId}")
    Customer getCustomer(@PathVariable("customerId")Long id){
          return customerService.getCustomer(id);

    }

@PostMapping

 void creatNewCustomer(@Valid @RequestBody Customer customer) {
    System.out.println("POST 请求。。。");
    System.out.println(customer);
}
@DeleteMapping(path = "{customerId}")
    void deleteCustomer(@PathVariable("customerId")Long id){
    System.out.println("Delete 请求，删除 Customer，其id为"+id);
}
@PutMapping
    void updateCustomer(@RequestBody Customer customer){
    System.out.println("UPDATE 请求");
    System.out.println(customer);
}
@GetMapping(path ="{customerId}/exception")

    Customer getCustomerException(@PathVariable("customerId")Long id){
        throw  new ApiRequestException("ApiRequestException for customer"+id);
}



}
